/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://github.com/mineadmin
 */
import type { MaFormItem } from "@mineadmin/form";
import hasAuth from "@/utils/permission/hasAuth.ts";
import MaDictSelect from "@/components/ma-dict-picker/ma-dict-select.vue";
import { concat } from "lodash-es";
import MaRemoteSelect from "@/components/ma-remote-select/index.vue";
import { page as getRodList } from "@/plugins/ai-warning/device/api/rdDeviceRod.ts";
import { page as getServerList } from "@/plugins/ai-warning/device/api/rdDeviceBroadcastServer.ts";

// 定义可编辑的字段列表
const EDITABLE_FIELDS = [
  "code",      // 设备MAC地址
  "name",      // 设备名称
  "rod_id",    // 装置名称
  "server_id", // 设备运行服务器
  "remarks",   // 备注
  "device_id", // 设备id
];

// 提取可编辑字段的函数
export function getEditableData(formData: any): any {
  const editableData: any = {};
  
  EDITABLE_FIELDS.forEach(field => {
    if (formData.hasOwnProperty(field)) {
      editableData[field] = formData[field];
    }
  });
  
  return editableData;
}

export default function getFormItems(
  formType: "add" | "edit" = "add",
  t: any,
  model: any
): MaFormItem[] {
  return concat([
    {
      label: "设备MAC地址",
      prop: "code",
      render: "input",
      renderProps: { placeholder: t("form.pleaseInput", { msg: "设备code" }) },
    },
    {
      label: "设备名称",
      prop: "name",
      render: "input",
      renderProps: { placeholder: t("form.pleaseInput", { msg: "设备名称" }) },
    },
    {
      label: "设备id",
      prop: "device_id",
      render: "input",
      renderProps: { placeholder: t("form.pleaseInput", { msg: "设备id" }) },
    },
    // {
    //   label: "设备别名",
    //   prop: "dev_name",
    //   render: "input",
    //   renderProps: { placeholder: t("form.pleaseInput", { msg: "设备别名" }) },
    // },
    {
      label: "装置名称",
      prop: "rod_id",
      render: () => MaRemoteSelect,
      renderProps: {
        api: getRodList,
        apiParams: {
          page: 1,
          pageSize: 1000,
        },
        dataHandle: (response: any) => {
          if (response.code === 200 && response.data && response.data.list) {
            return response.data.list.map((rod: any) => ({
              label: rod.name,
              value: rod.id,
            }));
          }
          return [];
        },
        placeholder: "请选择装置",
        filterable: true,
        clearable: true,
      },
      itemProps: { rules: [{ required: true, message: "请选择装置" }] },
    },
    {
      label: "设备运行服务器",
      prop: "server_id",
      render: () => MaRemoteSelect,
      renderProps: {
        api: getServerList,
        apiParams: {
          page: 1,
          pageSize: 1000,
        },
        dataHandle: (response: any) => {
          if (response.code === 200 && response.data && response.data.list) {
            return response.data.list.map((server: any) => ({
              label: server.name,
              value: server.id,
            }));
          }
          return [];
        },
        placeholder: "请选择设备运行服务器",
        filterable: true,
        clearable: true,
      },
      itemProps: {
        rules: [{ required: true, message: "请选择设备运行服务器" }],
      },
    },
    {
      label: "备注",
      prop: "remarks",
      render: "input",
      renderProps: { placeholder: t("form.pleaseInput", { msg: "备注" }) },
    },
  ]);
}
